import { creepCounter } from "../utils";


//移动到房间
//扫荡房间内所有creep
//拆除建筑
export = function roomAttacker(roomName: string):ICreepConfig{
  let targetCreep: Creep[];
  let targetStructure: Structure[];
  return {
    source: (creep: Creep) => {
      const room = Game.rooms[roomName];
      if (!room || creep.room.name != room.name) {
        creep.moveTo(new RoomPosition(20, 1, roomName));
      } else {
        creep.moveTo(new RoomPosition(20, 2, roomName));
        creepCounter((creep) => {
          targetCreep = creep.room.find(FIND_HOSTILE_CREEPS).sort((a, b) => {
            return creep.pos.getRangeTo(a) - creep.pos.getRangeTo(b);
          });
          if (_.isEmpty(targetCreep)) {
            targetStructure = creep.room.find(FIND_HOSTILE_CREEPS).sort((a, b) => {
              return creep.pos.getRangeTo(a) - creep.pos.getRangeTo(b);
            });
          }
        }, creep, 10);
      }
      if (!_.isEmpty(targetCreep)) {
        if (creep.attack(targetCreep[0]) == ERR_NOT_IN_RANGE) creep.moveTo(targetCreep[0]);
      } else if (!_.isEmpty(targetStructure)) {
        if (creep.attack(targetStructure[0]) == ERR_NOT_IN_RANGE) creep.moveTo(targetStructure[0]);
      } else {

      }
    },
    target: (creep: Creep) => {
      // this.source(creep);
    },
    switch: (creep: Creep) => false
  }
}